package util;

import java.awt.Color;
import java.util.Map;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.DatasetRenderingOrder;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

import server.TaskServer;

public class UI extends Thread {
	private static class UI_implements extends ApplicationFrame {

		private static final long serialVersionUID = 1L;

		private CategoryDataset dataset1;
		private CategoryDataset dataset2;
		private JFreeChart chart;

		public UI_implements(String title) {
			super(title);

			dataset1 = createDatasetColumns();
			dataset2 = createDatasetLine();
			this.chart = makeChart();

			ChartPanel chartPanel = new ChartPanel(chart);
			chartPanel.setPreferredSize(new java.awt.Dimension(800, 600));
			setContentPane(chartPanel);
		}

		private JFreeChart makeChart() {
			JFreeChart chart = ChartFactory.createBarChart("Estatistica", // chart
																			// title
					"ID", // x axis label
					"TAXA", // y axis label
					dataset1, // data
					PlotOrientation.VERTICAL, true, // include legend
					true, // tooltips
					false);

			chart.setBackgroundPaint(Color.WHITE);

			CategoryPlot plot = chart.getCategoryPlot();
			plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
			plot.setRangeAxisLocation(AxisLocation.TOP_OR_LEFT);

			plot.getRenderer().setSeriesPaint(0, Color.red);

			ValueAxis axis2 = new NumberAxis("Secondary");
			plot.setRangeAxis(1, axis2);
			plot.setDataset(1, dataset2);
			plot.mapDatasetToRangeAxis(1, 1);

			CategoryItemRenderer renderer = new LineAndShapeRenderer();
			renderer.setSeriesPaint(0, Color.blue);
			plot.setRenderer(1, renderer);
			plot.setDatasetRenderingOrder(DatasetRenderingOrder.REVERSE);

			return chart;
		}

		public void update() {
			this.dataset1 = createDatasetColumns();
			this.dataset2 = createDatasetLine();
			CategoryPlot plot = chart.getCategoryPlot();
			plot.setDataset(0, dataset1);
			plot.setDataset(1, dataset2);
		}

		private synchronized CategoryDataset createDatasetColumns() {
			DefaultCategoryDataset dataset = new DefaultCategoryDataset();
			String serie = "Rate";
		/*	for (Map.Entry<String, Statistically> entry : TaskServer.rates.entrySet())
				dataset.addValue(entry.getValue().rate, serie, entry.getKey());*/
			return dataset;
		}

		private synchronized CategoryDataset createDatasetLine() {
			DefaultCategoryDataset dataset = new DefaultCategoryDataset();
			String serie = "AVG";
			/*for (Map.Entry<String, Statistically> entry : TaskServer.rates.entrySet())
				dataset.addValue(entry.getValue().avg, serie, entry.getKey());*/
			return dataset;
		}

		public static void start() {
			UI_implements demo = new UI_implements("Statistically");
			demo.pack();
			RefineryUtilities.centerFrameOnScreen(demo);
			demo.setVisible(true);
			while (true) {
				try {
					demo.update();
					demo.repaint();
					Thread.sleep(1);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} 
			}
		}
	}
	@Override
	public void run() {
		UI_implements.start();		
	}
}
